<?php
/**
 * Created by PhpStorm.
 * User: 80496
 * Date: 2019/2/15
 * Time: 17:16
 */

namespace app\admin\controller;

use think\Session;
use think\Db;
class Index extends Visphp
{
    public function index()
    {
        $menu = Db::name('authmenu')->where(['is_display'=>1])->order('orders asc')->select();

        //删除不在当前角色权限里的菜单，实现隐藏
        $admin_cate = Session::get('admin_cate_id');
        $permissions = Db::name('membercate')->where(['id'=>$admin_cate])->value('permissions');
        $permissions = explode(',',$permissions);

        foreach ($menu as $k => $val) {
            if($val['type'] == 1 and $val['is_display'] == 1 and !in_array($val['id'],$permissions)) {
                unset($menu[$k]);
            }
        }

        //添加url
        foreach ($menu as $key => $value) {
            if(empty($value['parameter'])) {
                $url = url($value['module'].'/'.$value['controller'].'/'.$value['function']);
            } else {
                $url = url($value['module'].'/'.$value['controller'].'/'.$value['function'],$value['parameter']);
            }
            $menu[$key]['url'] = $url;
        }
        $menus = $this->menulist($menu);
        $this->assign('menus',$menus);

        $cookie = Db::name('member')->where('id',Session::get('user'))->find();
        $this->assign('cookie',$cookie);

        return view('/index');
    }
    public function main(){
        return view('/main');
    }
    protected function menulist($menu){
        $menus = array();
        //先找出顶级菜单
        foreach ($menu as $k => $val) {
            if($val['pid'] == 0) {
                $menus[$k] = $val;
            }
        }
        //通过顶级菜单找到下属的子菜单
        foreach ($menus as $k => $val) {
            foreach ($menu as $key => $value) {
                if($value['pid'] == $val['id']) {
                    $menus[$k]['list'][] = $value;
                }
            }
        }
        //三级菜单
        foreach ($menus as $k => $val) {
            if(isset($val['list'])) {
                foreach ($val['list'] as $ks => $vals) {
                    foreach ($menu as $key => $value) {
                        if($value['pid'] == $vals['id']) {
                            $menus[$k]['list'][$ks]['list'][] = $value;
                        }
                    }
                }
            }
        }//dump($menus);die;
        return $menus;
    }
}